package com.woniuxy.controller;

import com.woniuxy.entity.Order;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/07/31/星期四 10:33
 * @Component: 成都蜗牛学苑
 **/
@RestController
@RequestMapping("/order")
@RefreshScope //可以实时更新我们的配置文件，并生效
public class OrderController {
    @Autowired
    OrderService orderService;
    @Value("${woniu.ai.url}")
    String url;
    @Value("${woniu.ai.model}")
    String model;
    @GetMapping("/{oid}")
    public ResponseEntity getOrder(@PathVariable("oid") Integer oid){
        Order order = orderService.getOneById(oid);
        order.setUsername(order.getUsername()+url+";"+model);
        return new ResponseEntity(order);
    }
    /**
     * 新增订单的时候，拿到的是商品信息，远程调用Product服务，来扣减库存和新增订单
     * ChangeLog : 1. 创建 (25/07/31/星期四 11:36 [马宇航]);
     * @param pid
     * @param num
     * @return com.woniuxy.entity.ResponseEntity
    */
    @PostMapping("/{pid}/{num}")
    public ResponseEntity addOrder(@PathVariable("pid") Integer pid,
                                  @PathVariable("num") Integer num){
        try {
            Order save = orderService.save(pid, num);
            return new ResponseEntity(save);
        }catch (Exception e){
            return new ResponseEntity(500,e.getMessage());
        }
    }
}
